package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.impl.dao.WritDetailsDao;
import com.jiantong.lawEnforcing.model.WritDetails;
import com.jiantong.lawEnforcing.service.WritDetailsService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 文书内容
 * Created by mz03 on 2017/8/17.
 */
@Slf4j
@Service
@RpcProvider
public class WritDetailsServiceImpl implements WritDetailsService {

    private final WritDetailsDao writDetailsDao;

    @Autowired
    public WritDetailsServiceImpl(WritDetailsDao writDetailsDao) {
        this.writDetailsDao = writDetailsDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return writDetails
     */
    @Override
    public Response<WritDetails> finById(Long id) {
        try {
            WritDetails writDetails = writDetailsDao.findById(id);
            if (writDetails != null) {
                return Response.ok(writDetails);
            } else {
                log.error("writDetails (id={}) is not found", id);
                return Response.fail("writDetails.not.found");
            }
        }catch (Exception e){
            log.error("failed to find writDetails(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("writDetails.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param writDetails 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(WritDetails writDetails) {
        try {

            writDetailsDao.create(writDetails);
            return Response.ok(writDetails.getId());
        }catch (Exception e) {
            log.error("failed.to.create.writDetails:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.writDetails.failed");
        }
    }

    /**
     * 更新
     *
     * @param writDetails 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(WritDetails writDetails) {
        try{
            return Response.ok(writDetailsDao.update(writDetails));
        }catch (Exception e){
            log.error("failed.to.update.writDetails:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.writDetails.failed");
        }
    }

    /**
     * 根据文书类型 查询list
     *
     * @param type
     */
    @Override
    public Response<Paging<WritDetails>> findByType(Integer pageNo, Integer pageSize,Long cases_basic_id,Integer type) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(type != null){
                criteria.put("writ_type",type);
            }
            if(type != null){
                criteria.put("cases_basic_id",cases_basic_id);
            }
            return Response.ok(writDetailsDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find Paging WritDetails(type={}), cause:{}", type, Throwables.getStackTraceAsString(e));
            return Response.fail("paging WritDetails.find.fail");
        }
    }
}
